package com.sy.redis;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.SerializationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanResult;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class RedisClient {
    @Autowired
    private JedisPool jedisPool;

    private RedisClient() {

    }

    public boolean set(String key, String value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return "OK".equals(jedis.set(key, value));
        }
    }

    public String get(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.get(key);
        }
    }

    public ScanResult<String> scan(String cursor) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.scan(cursor);
        }
    }
    public Set<String> getKeys(String pattern) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.keys(pattern);
        }
    }
    public boolean setObj(String key, Serializable value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return "OK".equals(jedis.set(key.getBytes(), SerializationUtils.serialize(value)));
        }
    }

    public <T> T getObj(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            byte[] value = jedis.get(key.getBytes());
            if (!ArrayUtils.isEmpty(value)) {
                Object obj = SerializationUtils.deserialize(value);
                return (T) obj;
            }
        }
        return null;
    }

    public boolean setJson(String key, Object obj) {
        try (Jedis jedis = jedisPool.getResource()) {
            return "OK".equals(jedis.set(key, JSON.toJSONString(obj)));
        }
    }

    public <T> T getJsonObj(String key, Class<T> clazz) {
        try (Jedis jedis = jedisPool.getResource()) {
            //clazz是要转换出来的目标对象的类型
            return JSON.parseObject(jedis.get(key), clazz);
        }
    }

    public <T> List<T> getJsonObjList(String key, Class<T> clazz) {
        try (Jedis jedis = jedisPool.getResource()) {
            //clazz是目标集合中元素的泛型
            return JSON.parseArray(jedis.get(key), clazz);
        }
    }

    public void expire(String key, int expire) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.expire(key, expire);
        }
    }

    public boolean exists(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.exists(key);
        }
    }

    public boolean del(String... keys) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.del(keys) > 0;
        }
    }

    public boolean lpush(String key, String... values) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.lpush(key, values) > 0;
        }
    }

    public boolean lrem(String key, Long count, String value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.lrem(key, count, value) > 0;
        }
    }

    public List<String> lrange(String key, Long start, Long end) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.lrange(key, start, end);
        }
    }

    public Long llen(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.llen(key);
        }
    }

    public Long hincrby(String key, String field, Long value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hincrBy(key, field, value);
        }
    }

    public Map<String, String> hgetAll(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hgetAll(key);
        }
    }

    public Long hset(String key, String field, String value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hset(key, field, value);
        }
    }

    public Long hdel(String key, String... fields) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hdel(key, fields);
        }
    }

    public List<String> hmget(String key, String... fields) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hmget(key, fields);
        }
    }

    public Long incr(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.incr(key);
        }
    }
}
